/*
 * Copyright (c) 2005 -2008 Reliable Source, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Mozilla Public License v.1.1,
 * BSD, and Eclipse Public License v1.0 which accompanies this
 * distribution and is available at:
 *
 * 		http://www.eclipse.org/legal/epl-v10.html
 * 		http://www.opensource.org/licenses/bsd-license.php
 * 		http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * Contributors:
 *     IBM Corporation	     -  http://ibm.com
 *     	- initial API and implementation
 *
 *     patientos.org         - http://patientos.org
 *     	- domain and database persistence layer
 *
 *     Reliable Source, Inc. - http://reliablesource.org
 *     	- Symbiosys Application Framework,
 *     	- Symbiosys Application, Symbiosys Product
 *
 */
package org.reliablesource.client.ui.symbiosys.platform;

import com.patientis.business.security.InvalidUserLoginException;
import com.patientis.business.security.InvalidUserPasswordException;

import com.patientis.client.security.login.LoginController;
import com.patientis.client.security.login.LoginModel;

import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;

import org.eclipse.swt.widgets.Display;

import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;

/**
 *
 * Application.java<br>
 * Created on: Jun 27, 2008<br>
 *
 * @author <a href="mailto:dennis.park@gmail.com">Dennis Park</a>
 *
 * @category
 * @since 1.0
 */
public class SymbiosysApplication implements IApplication {
    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext)
     */
    public Object start(IApplicationContext context) {
	Display display = PlatformUI.createDisplay();
	connect();

	try {
	    int returnCode = PlatformUI.createAndRunWorkbench(display,
		    new ApplicationWorkbenchAdvisor());

	    if (returnCode == PlatformUI.RETURN_RESTART) {
		return IApplication.EXIT_RESTART;
	    }

	    return IApplication.EXIT_OK;
	} finally {
	    display.dispose();
	}
    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.equinox.app.IApplication#stop()
     */
    public void stop() {
	final IWorkbench workbench = PlatformUI.getWorkbench();

	if (workbench == null)
	    return;

	final Display display = workbench.getDisplay();
	display.syncExec(new Runnable() {
	    public void run() {
		if (!display.isDisposed())
		    workbench.close();
	    }
	});
    }

    static public void connect() {
	System.setProperty("java.naming.provider.url", "localhost:7107");
	System.setProperty("java.naming.factory.url.pkgs",
		"org.jboss.naming:org.jnp.interfaces");
	System.setProperty("java.naming.factory.initial",
		"org.jnp.interfaces.NamingContextFactory");
	System.setProperty("patientos.cache.subdir", "demo");

	// UserModel _user = SecurityService.authenticate("DEMO", "demo");
	// UserModel _appUser = new UserModel();
	// _appUser.copyAllFrom(_user);
	try {
	    LoginController.authenticateUser(new LoginModel("DEMO", "demo"));
	} catch (InvalidUserLoginException e) {
	    e.printStackTrace();
	} catch (InvalidUserPasswordException e) {
	    e.printStackTrace();
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }
}
